package work;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import lombok.Data;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import templateExecel2.fram.Renderer;
import templateExecel2.utils.PoiUtils;
import work.config.Constant;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Stream;

public class 使用Swagger提取Api文档 {

    public static void main(String[] args) throws IOException {

        //http://192.168.10.101:8213/mdp/v2/api-docs
        //创建一个请求
        CloseableHttpClient client = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet("http://192.168.10.101:8213/mdp/v2/api-docs");
        CloseableHttpResponse response = client.execute(httpGet);

        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity);
        response.close();

        JSONObject jsonObject = JSONObject.parseObject(string);

        Map<String, Path> pathMap = new HashMap<>();

        Map<String, Controller> controllerMap = new HashMap<>();

        //获取所有路径
        JSONObject paths = jsonObject.getJSONObject("paths");

        //路径
        for (String path : paths.keySet()) {

            //获取路径json
            JSONObject pathObj = paths.getJSONObject(path);

            //遍历所有请求方法
            for (String method : pathObj.keySet()) {

                //请求方法
                JSONObject methodObj = pathObj.getJSONObject(method);

                //获取接口的说明
                String summary = methodObj.getString("summary");

                //对请求方法的说明
                JSONArray tags = methodObj.getJSONArray("tags");

                out:
                for (Object tag : tags) {
                    if (tag != null) {
                        String s = tag.toString();
                        Method m = new Method();
                        m.setMethod(method.toUpperCase());
                        m.setPath(path);
                        m.setTag(s);
                        m.setSummary(summary);

                        Path pathObject = pathMap.computeIfAbsent(path, k -> new Path());

                        if (pathObject.getMethods() == null) {
                            pathObject.setTag(m.getTag());
                            pathObject.setPath(m.getPath());
                            pathObject.setMethods(new ArrayList<>());
                        }

                        pathObject.getMethods().add(m);

                        Controller controller = controllerMap.computeIfAbsent(pathObject.getTag(), k -> new Controller());

                        if (controller.getPaths() == null) {
                            controller.setTag(pathObject.getTag());
                            controller.setPaths(new ArrayList<>());
                        }

                        if (!controller.getPaths().contains(pathObject)) {
                            controller.getPaths().add(pathObject);
                        }

                    }
                }

            }

        }
        String[] tn = (
                "tc_md_communist\n" +
                        "tc_md_communist_party\n").split("\n");


        List all = new ArrayList<>();

        //遍历所有名称
        for (String name : controllerMap.keySet()) {

            boolean flag = false;

            String join = null;

            for (String s : tn) {

                Stream<String> stream = Arrays.stream(s.split("_")).map(v -> v.substring(0, 1).toUpperCase() + v.substring(1));

                Object[] array = stream.toArray();

                //将表名变成大写
                join = Joiner.on("").join(array);

                if (name.contains(join)) {
                    flag = true;
                    break;
                }
            }

            //判断是否应该包含
            if (!flag) {
                continue;
            }

            Controller controller = controllerMap.get(name);
            for (Path path : controller.getPaths()) {
                for (Method method : path.getMethods()) {
                    method.setModel(join);
                }
            }
            all.add(controller);

        }



        Map<String, Object> data = new HashMap<>();
        data.put("list", all);

        XSSFWorkbook workbook = PoiUtils.read(new File(Constant.work, "目标文件.xlsx"));

        XSSFSheet template = workbook.getSheet("接口文档");
        XSSFSheet target = workbook.createSheet("接口文档生成");

        Renderer.render(data, template, target);

        PoiUtils.write(workbook, new File(Constant.work, "目标文件1.xlsx"));
    }


    @Data
    public static class Controller {
        //标记
        private String tag;
        private List<Path> paths;
    }

    @Data
    public static class Path {
        private String path;
        private String tag;
        private List<Method> methods;
    }

    @Data
    public static class Method {
        private String method;
        private String summary;
        private String path;
        private String tag;
        private String model;

    }


}
